package com.stacker.management.application;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Arrays;

@RestController
public class LocalAppApplication {
    @Autowired
    RestTemplate stackerRestTemplate;

    @RequestMapping("/managemenet/apps/download")
    public void downloadRequireDocument(
            HttpServletRequest request, HttpServletResponse response) {
        String filePath = "/tmp/test";
        response.setHeader("Content-Disposition", "attachment;filename=abc");

        try (InputStream is = new FileInputStream(filePath);
             OutputStream os = response.getOutputStream()) {
            int read = 0;
            byte[] bytes = new byte[2048];

            while ((read = is.read(bytes)) != -1) {//按字节逐个写入，避免内存占用过高
                os.write(bytes, 0, read);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
